package com.fxl.majiang.room;

import com.fxl.majiang.exception.CustomException;
import com.fxl.majiang.websocket.ClientManager;
import lombok.SneakyThrows;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fengxinglei
 * @date 2022/8/1 18:09
 * @description 房间管理
 */
public class RoomManager {

    // 所有房间列表
    private static final ConcurrentHashMap<String,Room> rooms = new ConcurrentHashMap<>();

    // 用户id和房间id关系
    private static final ConcurrentHashMap<String,String> userAndRoomRelation = new ConcurrentHashMap<>();

    // 可容纳的最大房间数
    private static final int MAXIMUM_ROOMS = 1;

    /**
     * 获取用户所在房间
     * @param userId
     * @return
     */
    @SneakyThrows
    public static Room getUserRoom(String userId) {
        String roomId = getUserRoomId(userId);
        if (!StringUtils.hasText(roomId)) {
            return null;
        }
        return rooms.get(roomId);
    }

    /**
     * 给用户分配房间
     * @param userId
     * @return 房间id
     */
    public synchronized static String distribution(String userId) {
        // 没房间则创建房间
        if (rooms.size() == 0) {
            return createRoom(userId);
        }
        // 遍历现有房间，查找是否有合适的
        for(Room _room : rooms.values()) {
            if (_room.canJoin()) {
                String roomId = _room.join(userId);
                if (StringUtils.hasText(roomId)) {
                    // 绑定用户id与房间id
                    userAndRoomRelation.put(userId, roomId);
                    return roomId;
                }
            }
        }
        // 如果目前房间找不到合适的，并且房间池还充足，则创建一个
        if (rooms.size() < MAXIMUM_ROOMS) {
            return createRoom(userId);
        }
        return null;
    }

    /**
     * 将用户分配到某个房间
     * @param userId
     * @param roomId
     * @return
     */
    public synchronized static String distributionToRoom(String userId, String roomId) {
        Room room = rooms.get(roomId);
        if (room != null) {
            if (room.canJoin()) {
                room.join(userId);
                // 绑定用户id与房间id
                userAndRoomRelation.put(userId, roomId);
                return roomId;
            }
        }
        return null;
    }

    /**
     * 创建房间
     * @param userId
     * @return
     */
    private static String createRoom(String userId) {
        Room room = new Room();
        String roomId = room.join(userId);
        if (StringUtils.hasText(roomId)) {
            rooms.put(roomId, room);
            userAndRoomRelation.put(userId, roomId);
            return roomId;
        }
        return null;
    }

    /**
     * 获取用户所在房间id
     * @param userId
     * @return
     */
    public static String getUserRoomId(String userId) {
        if (CollectionUtils.isEmpty(userAndRoomRelation)) {
            return null;
        }
        return userAndRoomRelation.get(userId);
    }

    /**
     * 移除用户与房间的关系
     * @param userId
     */
    public static void removeFromRoom(String userId) {
        userAndRoomRelation.remove(userId);
    }

    /**
     * 清理所有房间
     */
    public static void clearAllRooms() {
        userAndRoomRelation.clear();
        rooms.clear();
        ClientManager.clearAll();
    }
}